package com.usvisatrack.core.factories;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.DataObject;
import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.CmsArticle;
import com.usvisatrack.core.dao.model.CmsArticleOssezContent;
import com.usvisatrack.core.dao.model.SocialMediaLog;
import com.usvisatrack.core.dao.model.UscisCase;
import com.usvisatrack.core.dao.model.VisaStatusLog;

/**
 * 
 * 
 * @author YuCheng Hu
 *
 */
public class DataFactory {
	// private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;
	private static Logger logger = LoggerFactory.getLogger(DataFactory.class);

	/**
	 * 
	 * @param id
	 * @return
	 */
	public static CmsArticleOssezContent getCmsArticleOssezContent(Long aid) {
		CmsArticleOssezContent cmsArticleOssezContent = null;

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(CmsArticleOssezContent.class);
			criteria.add(Restrictions.eq("aid", aid));
			criteria.setMaxResults(1);
			criteria.uniqueResult();

			cmsArticleOssezContent = (CmsArticleOssezContent) criteria.uniqueResult();
		} catch (Exception ex) {
			cmsArticleOssezContent = null;
			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.commitTransaction();
		}

		return cmsArticleOssezContent;
	}

	public static CmsArticle getCmsArticle(String rssArticleId) {
		CmsArticle cmsArticle = null;
		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(CmsArticle.class);
			criteria.add(Restrictions.eq("rssArticleId", rssArticleId));
			criteria.setMaxResults(1);
			criteria.uniqueResult();

			cmsArticle = (CmsArticle) criteria.uniqueResult();
		} catch (Exception ex) {

			logger.error("Get Visa by item ID error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return cmsArticle;
	}

	public static void save(DataObject obj) {
		obj.setModifyDate(new Date());
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(obj);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Save CheckeeVisa OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}

}